/*
 * Date         Author     Note
 * 2014.12.01   wzwz       程序升级
 * 2015.06.24   wzwz       增加返回按钮
 * 2015.06.26   wzwz       升级前推出运行模式
 * 2015.06.26   wzwz       增加STM32升级
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <sys/stat.h>
#include "resource.h"
#include "func.h"
#include "lang.h"
#include "mhbtn.h"
#include "mhlabel.h"
#include "mhctrlstate.h"
#include "mhedit.h"
#include "mhstatic.h"
#include "update.h"
#include "com.h"
#include "progress.h"
#include "info.h"
#include "globe.h"
#include "update_net.h"

#define SYS1_UPDATE     "mh1_update_net.tar.gz"
#define SYS2_UPDATE     "mh2_update_net.tar.gz"
#define SYS1_UPDATE_DIR "mh1_update_net"
#define SYS2_UPDATE_DIR "mh2_update_net"
#define  MAX_LINE_BYTE  200
/*
 *
 */
static void CreateUpdateInterface(HWND hWnd)
{
    CreateWindow(MH_BTN_NAME, GetString(780), WS_CHILD | BS_FLAT | BS_LEFT
            | WS_VISIBLE, ID_BUTTON1, 30, 60, 200, 20, hWnd, 0);
    CreateWindow(MH_BTN_NAME, GetString(781), WS_CHILD | BS_FLAT | BS_LEFT
            | WS_VISIBLE, ID_BUTTON2, 30, 90, 200, 30, hWnd, 0);
    CreateWindow(MH_BTN_NAME, GetString(782), WS_CHILD | BS_FLAT | BS_LEFT
            | WS_VISIBLE, ID_BUTTON3, 30, 120, 200, 30, hWnd, 0);
    CreateWindow(MH_BTN_NAME, GetString(783), WS_CHILD | BS_FLAT | BS_LEFT
            | WS_VISIBLE, ID_BUTTON4, 30, 150, 200, 30, hWnd, 0);
    CreateWindow(MH_BTN_NAME, GetString(803), WS_CHILD | BS_FLAT | BS_LEFT
            | WS_VISIBLE, ID_BUTTON5, 30, 180, 200, 30, hWnd, 0);
    char esc[64] = { 0 };
    sprintf(esc, GetString(802), 6);
    CreateWindow(MH_BTN_NAME, esc, WS_CHILD | BS_FLAT | BS_LEFT | WS_VISIBLE,
            ID_ESC, 30, 210, 200, 30, hWnd, 0);
}
/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{
    switch (nID)
    {
    case ID_BUTTON1:
        if(STATE_STOP != GetRunState(g_knit_data))
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        else if(HasUsb())
        {
            char file[MH_FILE_NAME_MAX_LEN] = { 0 };
            strcat(file, GetUsbDir());
            strcat(file, UPDATE_2812_FILE);
            CreateProgressWindow(hWnd, file, UPDATE_TYPE_APP);
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(555));
        }
        break;
    case ID_BUTTON2:
        if(STATE_STOP != GetRunState(g_knit_data))
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        else if(HasUsb())
        {
            char file[MH_FILE_NAME_MAX_LEN] = { 0 };
            strcat(file, GetUsbDir());
            strcat(file, UPDATE_2812_FILE);
            CreateProgressWindow(hWnd, file, UPDATE_TYPE_RACK);
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(555));
        }
        break;
    case ID_BUTTON3:
        if(STATE_STOP != GetRunState(g_knit_data))
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        else if(HasUsb())
        {

            char file[MH_FILE_NAME_MAX_LEN] = { 0 };
            strcat(file, GetUsbDir());
            VERSION version;
            memset(&version, 0, sizeof(VERSION));
            if(GetVersionInfo(g_knit_data, &version))
            {
                printf("failed to get version\n");
            }
            if((int)(version.header[0] & 0x0F) >= 3) // 新的双系统机头板  >= 2.3.xx
            {
                strcat(file, UPDATE_STM32_DSCRGFN);
            }
            else
            {
                strcat(file, UPDATE_SSSC_2407_FILE);
            }
            CreateProgressWindow(hWnd, file, UPDATE_TYPE_HEADER);
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(555));
        }
        break;
    case ID_BUTTON4:
        if(STATE_STOP != GetRunState(g_knit_data))
        {
            AfxInfoBox(hWnd, NULL, GetString(461));
        }
        else if(HasUsb())
        {
            char file[MH_FILE_NAME_MAX_LEN] = { 0 };
            strcat(file, GetUsbDir());
            strcat(file, UPDATE_2802_FILE);
            CreateProgressWindow(hWnd, file, UPDATE_TYPE_BOARD);
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(555));
        }
        break;
    case ID_BUTTON5:
        if(STATE_STOP != GetRunState(g_knit_data))
        {
          AfxInfoBox(hWnd, NULL, GetString(461));
        }
        else if(HasUsb())
        {
            char file[MH_FILE_NAME_MAX_LEN] = { 0 };
            strcat(file, GetUsbDir());
            strcat(file, UPDATE_STM32_GAUZE_FILE);
            CreateProgressWindow(hWnd, file, UPDATE_TYPE_GAUZE);
        }
        else
        {
            AfxInfoBox(hWnd, NULL, GetString(555));
        }
        break;
    case ID_ESC:
        {
            PostMessage(hWnd, MSG_CLOSE, 0, 0);
        }
    default:
        break;
    }
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE || key == SCANCODE_6)
    {
        OnBtnCmd(hWnd, ID_ESC);
    }
    else if(key == SCANCODE_1)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BUTTON1));
        OnBtnCmd(hWnd, ID_BUTTON1);
    }
    else if(key == SCANCODE_2)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BUTTON2));
        OnBtnCmd(hWnd, ID_BUTTON2);
    }
    else if(key == SCANCODE_3)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BUTTON3));
        OnBtnCmd(hWnd, ID_BUTTON3);
    }
    else if(key == SCANCODE_4)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BUTTON4));
        OnBtnCmd(hWnd, ID_BUTTON4);
    }

    else if(key == SCANCODE_5)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BUTTON5));
        OnBtnCmd(hWnd, ID_BUTTON5);
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID >= ID_BUTTON1 && nID < ID_BUTTON5)
        {
            ++nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
        else if(nID == ID_BUTTON5)
        {
            nID = ID_ESC;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
        if(nID > ID_BUTTON1 && nID <= ID_BUTTON5)
        {
            --nID;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
        else if(nID == ID_ESC)
        {
            nID = ID_BUTTON5;
            SetFocusChild(GetDlgItem(hWnd, nID));
        }
    }
    else if(key == SCANCODE_ENTER)
    {
        OnBtnCmd(hWnd, GetDlgCtrlID(GetFocusChild(hWnd)));
    }
}
/*
 *
 */
static int UpdateWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case MSG_CREATE:
        {
            CreateUpdateInterface(hWnd);
        }
        break;
    case MSG_PAINT:
        {
            HDC hdc = GetClientDC(hWnd);
            RECT rect;
            GetClientRect(hWnd, &rect);
            rect.left += 100;
            rect.top += MASTER_TITLE_TOP;
            rect.right -= 100;
            rect.bottom = rect.top + MASTER_TITLE_HEIGHT - 1;
            SetBkMode(hdc, BM_TRANSPARENT);
            SetTextColor(hdc, COLOR_HQ_YELLOW(hdc));
            DrawText(hdc, GetWindowCaption(hWnd), -1, &rect, DT_CENTER
                    | DT_VCENTER | DT_SINGLELINE);
            ReleaseDC(hdc);
        }
        break;
    case MSG_CLOSE:
        {
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
        }
        break;
    case MSG_KEYDOWN:
        {
            OnKeyDown(hWnd, wParam);
        }
        break;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*) lParam, &g_bmp[BMP_MAST]);
        }
        return 0;
    case MSG_COMMAND:
        {
            OnBtnCmd(hWnd, LOWORD(wParam));
        }
        return 0;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateUpdateWindow(HWND hHosting)
{
    int x = (CX_SCREEN - MASTER_WND_CX) / 2;
    int y = (CY_SCREEN - MASTER_WND_CY) / 2;

    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = GetString(250);
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = UpdateWinProc;
    CreateInfo.lx = x;
    CreateInfo.ty = y;
    CreateInfo.rx = x + MASTER_WND_CX;
    CreateInfo.by = y + MASTER_WND_CY;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        SetFocusChild(GetDlgItem(hWnd, ID_BUTTON1));
        ShowWindow(hWnd, SW_SHOWNORMAL);
    }
    return hWnd;
}
